package com.zwy.Algorithm.sort;


//插入排序
public class InsertionSort {
    public static void main(String[] args) {
        InsertionSort.Solution solution = new InsertionSort().new Solution();
        int []arr={7,8,9,4,3,1,10,6,5};
         solution.sort(arr);
        System.out.println(arr);
    }
    class Solution {
        public  void sort(int[] arr){

            int n = arr.length;
            for (int i = 0; i < n; i++) {
                // 寻找元素 arr[i] 合适的插入位置
                for( int j = i ; j > 0 ; j -- )
                    if( arr[j]<arr[j-1] )
                        swap( arr, j , j-1 );
                    else
                        break;
            }
        }
        //核心代码---结束
        private  void swap(int[] arr, int i, int j) {
            int t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
        }

    }
}
